import axios from 'axios'
const imgUrl = 'http://localhost:7070';


export default {
    // 后端的图片存放链接
    imgUrl,

    // 获得所有商品数据
    async getAllGoods() {
        let ret;
        await axios.get("/goods/")
            .then(response => {
                ret = response.data;
            })
        return ret;
    },
    // 按照分类查看商品
    async getGoodsByType(id) {
        let ret;
        await axios.get("/goods/type/" + id)
            .then(response => {
                ret = response.data;
            })
        return ret;
    },
    // 按照商品id查看商品
    async getGoodsById(id) {
        let ret;
        await axios.get("/goods/" + id)
            .then(response => {
                ret = response.data;
            })
        return ret;
    },
    // 删除商品
    async removeGoods(id) {
        let ret;
        await axios.delete("/goods/" + id)
            .then(response => {
                ret = response.data;
            })
        return ret;
    },
    // 按照商品名查找商品（模糊匹配）
    async searchGoodsByName(searchName) {
        let ret;
        await axios.get("/goods/name/" + searchName)
            .then(response => {
                ret = response.data;
            })
        return ret;
    },
    // 添加商品
    async addGoods(inputGoods) {
        let ret;
        await axios.put("/goods/", inputGoods.value)
            .then(response => {
                ret = response.data;
            })
        return ret;
    },
    // 更新商品
    async updateGoods(inputGoods) {
        let ret;
        await axios.post("/goods/", inputGoods.value)
            .then(response => {
                ret = response.data;
            })
        return ret;
    },

    // 文件上传
    async fileUpload(searchName) {
        let ret;
        await axios.get("/goods/name/" + searchName)
            .then(response => {
                ret = response.data;
            })
        return ret;
    },

    // 用户注册
    async userRegister(inputUser) {
        let ret;
        await axios.post("/users/register", inputUser.value)
            .then(response => {
                ret = response.data;
            })
        return ret;
    },
    // 用户登录
    async userLogin(inputUser) {
        let ret;
        await axios.post("/users/login", inputUser.value)
            .then(response => {
                ret = response.data;
            })
        return ret;
    },
    // 修改地址
    async updateAddress(inputUser) {
        console.log(inputUser)
        let ret;
        await axios.post("/users/address", inputUser)
            .then(response => {
                ret = response.data;
            })
        return ret;
    },

    // 添加推荐信息
    async addRecommend(inputRecommend) {
        let ret;
        await axios.put("/recommends/", inputRecommend.value)
            .then(response => {
                ret = response.data;
            })
        return ret;
    },
    // 删除推荐信息
    async removeRecommend(type, goodsId) {
        let ret;
        await axios.delete("/recommends/" + type + "/" + goodsId)
            .then(response => {
                ret = response.data;
            })
        return ret;
    },
    // 按照推荐类型查看商品数据
    async getRecommend(type) {
        let ret;
        await axios.get("/recommends/" + type)
            .then(response => {
                ret = response.data;
            })
        return ret;
    },

    // 查看购物车内商品详情
    async showShopCarGoods(userId) {
        let ret;
        await axios.get("/shopcars/goods/" + userId)
            .then(response => {
                ret = response.data;
            })
        return ret;
    },
    // 查看购物车信息
    async showShopCar(userId) {
        let ret;
        await axios.get("/shopcars/" + userId)
            .then(response => {
                ret = response.data;
            })
        return ret;
    },
    // 添加购物车（封装）
    async addshopcar(goodId, userId) {
        let search;
        let ret;
        // 查看购物车信息
        await axios.get("/shopcars/" + userId)
            .then(response => {
                search = response.data;
            })
        // 循环查找是否已经存在相同条目
        for (let i = 0; i < search.result.length; i++) {
            if (search.length == 0) {
                break;
            }
            console.log(search.result[i].goodsId)
            if (search.result[i].goodsId == goodId) {
                // 如果有，直接修改其值+1
                await axios.get("/shopcars/" + search.result[i].id + "/1")
                    .then(response => {
                        ret = response.data;
                    })
                return ret;
            }
        }
        // 如果一直没有，添加新条目
        await axios.put("/shopcars/" + userId + "/" + goodId)
            .then(response => {
                ret = response.data;
            })
        return ret;
    },
    // 添加购物车条目
    async addShopCar(userId, goodsId) {
        let ret;
        await axios.put("/shopcars/" + userId + "/" + goodsId)
            .then(response => {
                ret = response.data;
            })
        return ret;
    },
    // 修改购物车中已有商品的数量
    async changeAmount(shopCarId, amount) {
        let ret;
        await axios.get("/shopcars/" + shopCarId + "/" + amount)
            .then(response => {
                ret = response.data;
            })
        return ret;
    },
    // 删除购物车中的一条记录
    async removeShopCar(shopCarId) {
        let ret;
        await axios.delete("/shopcars/" + shopCarId)
            .then(response => {
                ret = response.data;
            })
        return ret;
    },
    // 清空购物车
    async clearShopCar(userId) {
        let ret;
        await axios.delete("/shopcars/clear/" + userId)
            .then(response => {
                ret = response.data;
            })
        return ret;
    },

    // 查看分类名及其数字ID
    async showTypeList() {
        let ret;
        await axios.get("/types/")
            .then(response => {
                ret = response.data;
            })
        return ret;
    },
    // 按照id返回分类名
    async showTypeName(typeId) {
        let ret;
        await axios.get("/types/" + typeId)
            .then(response => {
                ret = response.data;
            })
        return ret;
    },

    // 创建订单
    async createOrder(userId) {
        let ret;
        await axios.get("/orders/create/" + userId)
            .then(response => {
                ret = response.data;
            })
        return ret;
    },
    // 查询用户的所有订单
    async showAllOrders(userId) {
        let ret;
        await axios.get("/orders/" + userId)
            .then(response => {
                ret = response.data;
            })
        return ret;
    },
    // 修改支付状态
    async orderPay(orderId, paytype) {
        let ret;
        await axios.get("/orders/paytype/" + orderId + "/" + paytype)
            .then(response => {
                ret = response.data;
            })
        return ret;
    },
    // 修改发货状态
    async orderStatus(orderId, status) {
        let ret;
        await axios.get("/orders/status/" + orderId + "/" + status)
            .then(response => {
                ret = response.data;
            })
        return ret;
    },
    // 查看订单详情
    async showOrderitem(orderId) {
        let ret;
        await axios.get("/orders/orderitem/" + orderId)
            .then(response => {
                ret = response.data;
            })
        return ret;
    },
    async showAllOrder() {
        let ret;
        await axios.get("/orders/")
            .then(response => {
                ret = response.data;
            })
        return ret;
    }
}